package com.yfree.listeners;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.yfree.YConfig;
import com.yfree.activities.YActivity;
import com.yfree.utils.net.YJsonKV;
import com.yfree.utils.net.YResponseUtils;
import com.yfree.views.YProgressDialog;

import net.tsz.afinal.http.AjaxCallBack;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;

public abstract class YAjaxCallBack<T> extends AjaxCallBack<Object> {
    private YActivity yContext;
    private boolean showProgress = true;
    private boolean showErrorToast = true;
    private String jkid;
    private YJsonKV jsonKv;

    public YAjaxCallBack(YActivity yContext) {
        this.yContext = yContext;
    }

    public YAjaxCallBack(YActivity yContext, boolean showErrorToast) {
        this.yContext = yContext;
        this.showErrorToast = showErrorToast;
    }

    @Override
    public final void onStart() {
        if (isAlive()) {
            yContext.yRunOnUIThread(new Runnable() {
                @Override
                public void run() {
                    if (isAlive()) {
                        yContext.yLog(jkid + ":request", TextUtils.isEmpty(jsonKv.toString()) ? "无入参" : jsonKv.toString());
                        if (showProgress) {
                            YProgressDialog.showProgress(yContext);
                        }
                    }
                }
            });
        }
    }

    @Override
    public final void onSuccess(final Object t) {
        if (isAlive()) {
            yContext.yRunOnUIThread(YConfig.yPostTimeDelay, new Runnable() {
                @Override
                public void run() {
                    if (isAlive()) {
                        yContext.yLog(jkid + ":responseSuccess", YResponseUtils.deCompress(t.toString()));
                        if (showProgress) {
                            YProgressDialog.dismissProgress(yContext);
                        }
                        if (!YResponseUtils.isSuccess(t)) {
                            if (showErrorToast) {
                                yContext.yToast(YResponseUtils.getErrorMessage(t));
                            }
                            onFail(t, null, YResponseUtils.getCode(t), YResponseUtils.getErrorMessage(t));
                        } else {
                            onSuccessed(YResponseUtils.getResultList(t, getType()));
                        }
                    }
                }
            });
        }
    }

    @Override
    public final void onFailure(final Throwable t, final int errorNo, final String strMsg) {
        if (isAlive()) {
            yContext.yRunOnUIThread(YConfig.yPostTimeDelay, new Runnable() {
                @Override
                public void run() {
                    if (isAlive()) {
                        yContext.yLog(jkid + ":responseFailure", "网络请求异常！！！	ErrorNo:" + errorNo + "		Msg:" + strMsg);
                        if (showProgress) {
                            YProgressDialog.dismissProgress(yContext);
                        }
                        if (showErrorToast) {
                            yContext.yToast(YConfig.DEBUG ? "网络请求异常！！！\nErrorNo:" + errorNo + "\nMsg:" + strMsg : "网络请求错误，请重试");
                        }
                        onFail(null, t, String.valueOf(errorNo), strMsg);
                    }
                }
            });
        }
    }

    private boolean isAlive() {
        try {
            return yContext != null && !yContext.isFinishing() && !yContext.isDestroyed();
        } catch (Exception e) {
            return yContext != null && !yContext.isFinishing();
        }
    }

    public abstract void onSuccessed(@NonNull ArrayList<T> results);

    public void onFail(Object response, Throwable throwable, final String code, final String message) {
    }

    private Class<T> getType() {
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean getShowProgress() {
        return this.showProgress;
    }

    public void setJkid(String jkid) {
        this.jkid = jkid;
    }

    public void setJsonKv(YJsonKV jsonKv) {
        this.jsonKv = jsonKv;
    }
}